struct GSOutput
{
	float2 tex : TEXCOORD;
	float4 pos : SV_POSITION;	
};

cbuffer screenDimensions : register(cb0)
{
	float4 screenWidthHeight;
};

cbuffer planeInfo : register(cb1)
{
	float4 widthHeightLocation;
	float4 scaleRotationDepth;
};

cbuffer srcRect : register(cb2)
{
	float4 leftTopRightBottom;
};

[maxvertexcount(4)]
void GS(
	point float4 input[1] : SV_POSITION, 
	inout TriangleStream< GSOutput > output
)
{
	GSOutput gsOut;

	float imgHalfHeight;
	float imgHalfWidth;
	float2 texCoords[4];

	float widthPerPixel = 2.0f / screenWidthHeight.x;
	float heightPerPixel = 2.0f / screenWidthHeight.y;	

	if(leftTopRightBottom.x == 0.0f &&
		leftTopRightBottom.y == 0.0f &&
		leftTopRightBottom.z == 0.0f &&
		leftTopRightBottom.w == 0.0f)
	{
		imgHalfWidth = (widthHeightLocation.x / 2.0f);
		imgHalfHeight = (widthHeightLocation.y / 2.0f);		

		texCoords[0] = float2(0.0f, 0.0f);
		texCoords[1] = float2(0.0f, 1.0f);
		texCoords[2] = float2(1.0f, 0.0f);
		texCoords[3] = float2(1.0f, 1.0f);
	}
	else
	{
		imgHalfWidth = ((leftTopRightBottom.z - leftTopRightBottom.x) / 2.0f);
		imgHalfHeight = ((leftTopRightBottom.y - leftTopRightBottom.w) / 2.0f);		

		float imageRatioWidth = 1.0f / widthHeightLocation.x;
		float imageRatioHeight = 1.0f / widthHeightLocation.y;

		float2 BL = float2((leftTopRightBottom.x * imageRatioWidth), 1 - (leftTopRightBottom.y * imageRatioHeight));
		float2 BR = float2((leftTopRightBottom.z * imageRatioWidth), 1 - (leftTopRightBottom.y * imageRatioHeight));
		float2 TL = float2((leftTopRightBottom.x * imageRatioWidth), 1 - (leftTopRightBottom.w * imageRatioHeight));
		float2 TR = float2((leftTopRightBottom.z * imageRatioWidth), 1 - (leftTopRightBottom.w * imageRatioHeight));

		texCoords[0] = BL;
		texCoords[1] = TL;
		texCoords[2] = BR;
		texCoords[3] = TR;
	}

	imgHalfWidth *= scaleRotationDepth.x;
	imgHalfHeight *= scaleRotationDepth.y;
	
	float centerPosX = (widthHeightLocation.z);
	float centerPosY = (widthHeightLocation.w);

	float4 topLeft		= float4(centerPosX - imgHalfWidth, centerPosY - imgHalfHeight, scaleRotationDepth.w, 1.0f);
	float4 topRight		= float4(centerPosX + imgHalfWidth, centerPosY - imgHalfHeight, scaleRotationDepth.w, 1.0f);
	float4 bottomLeft	= float4(centerPosX - imgHalfWidth, centerPosY + imgHalfHeight, scaleRotationDepth.w, 1.0f);
	float4 bottomRight	= float4(centerPosX + imgHalfWidth, centerPosY + imgHalfHeight, scaleRotationDepth.w, 1.0f);

 	if(scaleRotationDepth.z != 0.0f)
 	{
		float2 distance;

		distance.x = (topLeft.x - centerPosX);
		distance.y = (topLeft.y - centerPosY);
 		topLeft.x = (distance.x * cos(scaleRotationDepth.z) - distance.y * sin(scaleRotationDepth.z)) + centerPosX;
 		topLeft.y = (distance.y * cos(scaleRotationDepth.z) + distance.x * sin(scaleRotationDepth.z)) + centerPosY;

		distance.x = (topRight.x - centerPosX);
		distance.y = (topRight.y - centerPosY);
 		topRight.x = (distance.x * cos(scaleRotationDepth.z) - distance.y * sin(scaleRotationDepth.z)) + centerPosX;
 		topRight.y = (distance.y * cos(scaleRotationDepth.z) + distance.x * sin(scaleRotationDepth.z)) + centerPosY;

		distance.x = (bottomLeft.x - centerPosX);
		distance.y = (bottomLeft.y - centerPosY);
 		bottomLeft.x = (distance.x * cos(scaleRotationDepth.z) - distance.y * sin(scaleRotationDepth.z)) + centerPosX;
 		bottomLeft.y = (distance.y * cos(scaleRotationDepth.z) + distance.x * sin(scaleRotationDepth.z)) + centerPosY;

		distance.x = (bottomRight.x - centerPosX);
		distance.y = (bottomRight.y - centerPosY);
 		bottomRight.x = (distance.x * cos(scaleRotationDepth.z) - distance.y * sin(scaleRotationDepth.z)) + centerPosX;
 		bottomRight.y = (distance.y * cos(scaleRotationDepth.z) + distance.x * sin(scaleRotationDepth.z)) + centerPosY;
 	}

	topLeft.x = (topLeft.x * widthPerPixel) - 1;
	topLeft.y = (topLeft.y * heightPerPixel) - 1;
	topRight.x = (topRight.x * widthPerPixel) - 1;
	topRight.y = (topRight.y * heightPerPixel) - 1;
	bottomLeft.x = (bottomLeft.x * widthPerPixel) - 1;
	bottomLeft.y = (bottomLeft.y * heightPerPixel) - 1;
	bottomRight.x = (bottomRight.x * widthPerPixel) - 1;
	bottomRight.y = (bottomRight.y * heightPerPixel) - 1;

	gsOut.pos = bottomLeft;
	gsOut.tex = texCoords[0];
	output.Append(gsOut);
	
	gsOut.pos = topLeft;
	gsOut.tex = texCoords[1];
	output.Append(gsOut);
	
	gsOut.pos = bottomRight;
	gsOut.tex = texCoords[2];
	output.Append(gsOut);
	
	gsOut.pos = topRight;
	gsOut.tex = texCoords[3];
	output.Append(gsOut);
}